package com.kitty.game.fight.factory;

import com.kitty.common.utils.PositionUtils;
import com.kitty.game.fight.bean.Fight;
import com.kitty.game.fight.bean.PetMirrorFightObject;
import com.kitty.game.fight.bean.RoleMirrorFightObject;
import com.kitty.game.fight.factory.model.MirrorFight;
import com.kitty.game.fight.message.vo.FightMember;
import com.kitty.game.mirror.model.FightMirror;
import com.kitty.game.mirror.model.PetMirror;
import com.kitty.game.mirror.model.RoleMirror;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.model.product.entry.FightSide;
import com.kitty.game.utils.Const;

import java.util.List;
import com.kitty.game.fight.factory.FightFactory;

public class MirrorFightFactory extends FightFactory {
    @Override
    public Fight create(int mapId, Object... params) {
        Role role = (Role) params[0];
        byte fightType  = (Byte) params[1];
        int npcId = (Integer) params[2];

        List<FightMirror> fightMirrorList = null;
        FightMirror fightMirror = null;
        if (params[3] instanceof List) {
            fightMirrorList = (List<FightMirror>)params[3];
        } else {
            fightMirror = (FightMirror) params[3];
        }

        double propRate = 1.0;
        Object rate = role.popTempCache(Const.FIGHT_MIRROR_PROP_RATE_KEY);
        if (rate != null) {
            propRate = (Double) rate;
        }
        FightSide enemyFightSide = fightMirrorList != null ? getEnemyFightSide(fightMirrorList, propRate) : getEnemyFightSide(fightMirror, propRate);
        MirrorFight fight = new MirrorFight(fightType, npcId, role, enemyFightSide);
        return fight;
    }

    private FightSide getEnemyFightSide(List<FightMirror> fightMirrorList, double propRate) {
        FightSide fightSide = new FightSide();
        int index = 0;
        int monsterId = 2000000000;

        for (FightMirror fightMirror : fightMirrorList) {
            addRoleMirror(fightMirror.getRoleMirror(), propRate, fightSide, index, monsterId);
            index ++;
            monsterId++;
        }

        index = 5;
        for (FightMirror fightMirror : fightMirrorList) {
            addPetMirror(fightMirror.getPetMirror(), fightSide, index, monsterId);
            index ++;
            monsterId ++;
        }

        return fightSide;
    }

    private FightSide getEnemyFightSide(FightMirror fightMirror, double propRate) {
        FightSide fightSide = new FightSide();
        int index = 0;
        int monsterId = 2000000000;


        addRoleMirror(fightMirror.getRoleMirror(), propRate, fightSide, index, monsterId);

        index = 5;
        monsterId++;
        addPetMirror(fightMirror.getPetMirror(), fightSide, index, monsterId);

        return fightSide;
    }

    private void addRoleMirror(RoleMirror roleMirror, double propRate, FightSide fightSide, int index, int monsterId) {
        short pos = PositionUtils.positions.get(index);
        RoleMirrorFightObject fightObject = new RoleMirrorFightObject(roleMirror, propRate, pos, monsterId);
        fightSide.getFightObjectList().add(fightObject);
        FightMember fightMember = fightObject.asFightMember();
        fightSide.getFightMemberList().add(fightMember);
    }

    private void addPetMirror(PetMirror petMirror, FightSide fightSide, int index, int monsterId) {
        Short pos = PositionUtils.positions.get(index);
        PetMirrorFightObject fightObject = new PetMirrorFightObject(petMirror, monsterId, fightSide.getFightObjectList().get(0), pos, false);
        fightSide.getFightObjectList().add(fightObject);
        FightMember fightMember = fightObject.asFightMember();
        fightSide.getFightMemberList().add(fightMember);
    }
}
